const { json } = require("express");
const { sqlPromise } = require("../db/sqlPromise");
const turnTree = require("../util/turnTree");

exports.getCategories = async (req, res) => {
  let sql = "select cat_id, cat_name, cat_pid, cat_level from sp_category";

  if (req.query.type && req.query.pagenum && req.query.pagesize) {
    sql += ` where cat_level = ${req.query.type - 1} limit ${
      (req.query.pagenum - 1) * req.query.pagesize
    }, ${req.query.pagesize}`;
  } else if (req.query.type) {
    sql += ` where cat_level = ${req.query.type - 1}`;
  }

  let result = await sqlPromise(sql);

  res.send({
    data: turnTree(result, "cat_id", "cat_pid"),
    meta: {
      msg: "获取成功",
      status: 200,
    },
  });
};

exports.addCategories = async (req, res) => {
  let sql = "select * from sp_category where cat_name=?";
  let results = await sqlPromise(sql, [req.body.cat_name]);
  if (results.length > 0)
    return res.send({ status: 100, msg: "商品分类已存在" });

  let insertSql = `insert into sp_category (cat_pid,cat_name,cat_level) values ('${req.body.cat_pid}','${req.body.cat_name}','${req.body.cat_level}')`;
  let insertRes = await sqlPromise(insertSql);
  console.log(insertRes);

  if (insertRes.affectedRows != 1)
    return res.send({ msg: "添加失败 未知错误" });

  results = await sqlPromise(sql, [req.body.cat_name]);

  res.send({
    data: {
      cat_id: results[0].cat_id,
      cat_name: results[0].cat_name,
      cat_pid: results[0].cat_pid,
      cat_level: results[0].cat_level,
    },
    meta: {
      msg: "创建成功",
      status: 201,
    },
  });
};

exports.getCategory = async (req, res) => {
  let id = req.params.id;
  let sql = `select * from sp_category where cat_id=${id}`;
  let results = await sqlPromise(sql);
  res.send({
    data: {
      cat_id: results[0].cat_id,
      cat_name: results[0].cat_name,
      cat_pid: results[0].cat_pid,
      cat_level: results[0].cat_level,
    },
    meta: {
      msg: "获取成功",
      status: 200,
    },
  });
};

exports.editCategory = async (req, res) => {
  let sql = `UPDATE sp_category SET cat_name='${req.query.cat_name}' WHERE cat_id = '${req.params.id}'`;
  let results = await sqlPromise(sql);
  console.log(results);
  if (results.affectedRows != 1) return res.send({ msg: "修改失败 未知错误" });

  let findsql = `select * from sp_category where cat_id = '${req.params.id}'`;
  let findRes = await sqlPromise(findsql);

  res.send({
    data: {
      cat_id: findRes[0].cat_id,
      cat_name: findRes[0].cat_name,
      cat_pid: findRes[0].cat_pid,
      cat_level: findRes[0].cat_level,
    },
    meta: {
      msg: "更新成功",
      status: 200,
    },
  });
};

exports.delCategory = async (req, res) => {
  let sql = `UPDATE sp_category SET cat_deleted=1 WHERE cat_id = '${req.params.id}'`;
  let results = await sqlPromise(sql);
  console.log(results);
  if (results.affectedRows != 1) return res.send({ msg: "删除失败 未知错误" });

  res.send({
    data: null,
    meta: {
      msg: "删除成功",
      status: 200,
    },
  });
};
